package com.jsbb.util;

import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * List工具类
 *
 * @author w
 * @date 2021-03-05
 */
@Slf4j
public class ListUtils {

    /**
     * list分页
     *
     * @param list
     * @param pageNo
     * @param pageSize
     * @return
     */
    public static List pagination(List list, Integer pageNo, Integer pageSize) {
        if (list == null || list.size() == 0) {
            return null;
        }
        Integer count = list.size();
        Integer pageCount = 0;
        if (count % pageSize == 0) {
            pageCount = count / pageSize;
        } else {
            pageCount = count / pageSize + 1;
        }
        int fromIndex = 0;
        int toIndex = 0;
        if (!pageNo.equals(pageCount)) {
            fromIndex = (pageNo - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageNo - 1) * pageSize;
            toIndex = count;
        }
        List pageList = list.subList(fromIndex, toIndex);
        return pageList;
    }
}
